package com.example.controller;

import com.example.dto.BookDTO;
import com.example.pojo.Book;
import com.example.pojo.PageBean;
import com.example.pojo.Result;
import com.example.service.BookService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/books")
@RequiredArgsConstructor
public class BookController {

    private final BookService bookService;

    @PostMapping
    public Result insert(@RequestBody Book book) {
        log.info("新增图书，book:{}", book);
        bookService.insertWithCategory(book);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id) {
        log.info("删除图书，id:{}", id);
        bookService.deleteWithCategory(id);
        return Result.success();
    }

    @PutMapping
    public Result updateById(@RequestBody Book book) {
        log.info("更新图书，book:{}", book);
        bookService.updateWithCategory(book);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id) {
        log.info("查询图书，id:{}", id);
        Book book = bookService.getById(id);
        return Result.success(book);
    }

    @GetMapping
    public Result find(BookDTO bookDTO) {
        log.info("分页参数：{}", bookDTO);
        PageBean<Book> pageBean = bookService.find(bookDTO);
        return Result.success(pageBean);
    }
}
